﻿using Liuliu.ScriptEngine;
namespace GmControlTools
{
    /// <summary>
    /// 下线玩家
    /// </summary>
    internal class OfflineUser
    {
        /// <summary>
        /// 根据玩家名字下线玩家
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static bool OfflineUserByName(string userName) 
        {  
            DmPlugin dm = Program.dm;
            int pid = Program.process.Id;
            int Hwnd2 = (int)Program.process.MainWindowHandle;
            int Hwnd = dm.FindWindow("", $"Gm{pid}");
            dm.SetWindowSize(Hwnd, 1024, 768);
            dm.BindWindowEx(Hwnd, "gdi", "windows3", "windows", "", 0);//绑定窗体
            
            var dm_ret = dm.MoveToClick(211, 12);
            if (dm_ret)
            {
                Thread.Sleep(20);
                int menuWindow = FindWindowBySize(Hwnd, 155, 96);
                if (menuWindow > 0)
                {
                    //重新绑定窗口
                    dm.UnBindWindow();
                    dm.BindWindowEx(menuWindow, "gdi", "windows3", "windows", "", 0);
                }
                else {
                    Console.WriteLine("获取“踢掉玩家”菜单窗口失败");
                    //以最大化的方式激活一次窗口
                    dm.SetWindowState(Hwnd, 4);
                    Thread.Sleep(20);
                    dm.SetWindowState(Hwnd, 3);
                    Thread.Sleep(20);
                    return false;
                }
                //窗口绑定成功后点击菜单：踢掉玩家
                dm_ret = dm.MoveToClick(60, 13);
                if (dm_ret)
                {
                    Thread.Sleep(20);
                    int index = 0;
                    while (index <= 4)//最多找4次
                    {
                        int onlineWindow = dm.FindWindow("#32770", "踢掉玩家");
                        if (onlineWindow > 0)
                        {
                            dm.UnBindWindow();
                            dm_ret = dm.BindWindowEx(onlineWindow, "gdi", "windows3", "windows", "", 0);
                            if (dm_ret)
                            {
                                Thread.Sleep(20);
                                string nameHwnds = dm.EnumWindow(onlineWindow, "", "Edit", 2);
                                dm.SendString(Convert.ToInt32(nameHwnds), userName);

                                //模拟点击踢人
                                dm.MoveTo(104, 104);
                                dm.LeftClick();
                                dm.UnBindWindow();

                                Thread.Sleep(50);//等待操作响应
                                int notFoundWindow = dm.FindWindow("#32770", "gm_client");
                                if (notFoundWindow > 0)
                                {
                                    //未找到用户时，需要关闭弹出框
                                    dm_ret = dm.BindWindowEx(notFoundWindow, "gdi", "windows3", "windows", "", 0);
                                    if (dm_ret)
                                    {
                                        //模拟点击“确定”
                                        dm.MoveTo(100, 110);
                                        dm.LeftClick();
                                        dm.SetWindowState(onlineWindow, 0);
                                        dm.UnBindWindow();
                                        Console.WriteLine($"踢人：{userName}----{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
                                    }
                                    else
                                    {
                                        Console.WriteLine("踢人时未找到用户,绑定未找到弹出框失败");
                                        return false;
                                    }
                                }
                                else
                                {
                                    Console.WriteLine($"踢掉玩家：玩家{userName}已下线");
                                    return true;
                                }
                            }
                        }
                        index++;
                    }
                }
                else {
                    Console.WriteLine("打开“踢掉玩家”窗口失败");
                }
            }
            return false;
        }
        /// <summary>
        /// 查找指定进程所有可见窗口符合窗口尺寸的窗口句柄
        /// </summary>
        /// <param name="Hwnd">父级窗口句柄</param>
        /// <param name="width">待获取的窗口宽度</param>
        /// <param name="height">待获取的窗口高度</param>
        public static int FindWindowBySize(int Hwnd,int width,int height) 
        {
            int result = 0;
            string childHwnds = Program.dm.EnumWindowByProcess(Program.processName, "", "", 16);
            if (!string.IsNullOrEmpty(childHwnds)) {
                string[] hwndArr = childHwnds.Split(',');
                if (hwndArr.Length > 0)
                {
                    foreach (string hwnd in hwndArr)
                    {
                        int x1, y1;
                        Program.dm.GetClientSize(Convert.ToInt32(hwnd), out x1, out y1);

                        if (width == x1 && height == y1)
                        {
                            return Convert.ToInt32(hwnd);
                        }
                    }
                }
            }
            else
            {
                Program.dm.SetWindowState(Hwnd, 4);
                result = FindWindowBySize(Hwnd, 155, 96);
                Program.dm.SetWindowState(Hwnd, 3);
            }
            return result;
        }
    }
}
